<?php
class ControllerCommonMenu extends Controller {
    /**
     * 网站头部 产品分类菜单 category menu
     * @return string
     * @throws Exception
     */
	public function index() {
		$this->load->language('common/menu');

		// Menu
		$this->load->model('catalog/category');

		$data['categories'] = array();
		$data['home'] = $this->url->link('common/home');

        $this->load->model('catalog/product');

        $data['categories'] = $this->categoryChildrenData(0, '',2);

        $limit = $this->getCategoryLocaleLimit();
        if ($limit) {
            $data['categories'] = array_slice($data['categories'], 0, $limit);
        }

        // 新增 博客和新闻导航栏
        $data = $this->addPressAndBlog($data);

        return $this->load->view('common/menu', $data);
	}

    /**
     * @param $category_id
     * @param string $id_path
     * @param int $load_sub_level
     *
     * @return array
     *
     * @throws Exception
     */
    private function categoryChildrenData($category_id, $id_path = '', $load_sub_level = 0)
    {
        /** @var ModelCatalogCategory $modelCatalogCategory */
        $modelCatalogCategory = $this->loadModel('catalog/category');
        $categories = $modelCatalogCategory->getCategories($category_id);

        $categories_data = [];

        foreach ($categories as $category) {
            $filter_data = [
                'filter_category_id'  => $category['category_id'],
                'filter_sub_category' => true
            ];

            $current_path = $id_path ? $id_path . '_' . $category['category_id'] : $category['category_id'];

            $categories_data[] = [
                'name'     => $category['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
                'href'     => $this->url->link('product/category', 'path=' . $current_path),
                'column'   => $category['column'] ?: 1,
                'children' => ($load_sub_level > 0)
                    ? $this->categoryChildrenData($category['category_id'], $current_path, $load_sub_level - 1)
                    : null,
            ];
        }

        return $categories_data;
    }

    /**
     * @param $data
     *
     * @return int|null
     *
     * @throws Exception
     */
    private function getCategoryLocaleLimit()
    {
        /** @var ModelLocalisationLanguage $localeLang */
        $localeLang = $this->loadModel('localisation/language');
        $lang = $localeLang->getLanguageByCode($this->session->data['language']);
        if (! isset($lang['language_id'])) {
            return null;
        }

        /** @var ModelSettingSetting $settingSetting */
        $settingSetting = $this->loadModel('setting/setting');
        $theme_setting = $settingSetting->getSetting('theme_default', $this->config->get('config_store_id'));

        $category_limit_key = 'theme_default_nav_category_locale_limit';
        $limit = isset($theme_setting[$category_limit_key][$lang['language_id']])
            ? (int) $theme_setting[$category_limit_key][$lang['language_id']]
            : null;

        return $limit;
    }

    /**
     * @param array $data
     * @return array
     */
    private function addPressAndBlog(array $data)
    {
//        $blog = [ // PM: hide blog temp
//            'name'     => $this->language->get('text_menu_blog'),
//            'href'     => $this->url->link('blog/all'),
//            'column'   => 1,
//            'children' => null,
//        ];

        $press = [
            'name'     => $this->language->get('text_menu_press'),
            'href'     => $this->url->link('press/all'),
            'column'   => 1,
            'children' => [],
        ];

        array_push($data['categories'],
//            $blog,
            $press);

        return $data;
    }
}
